//
//  HSAwardListCell.m
//  HolyShit
//
//  Created by Robin on 2017/3/31.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAwardListCell.h"

@interface HSAwardListCell ()

@property (nonatomic, strong) UILabel *nameLb;
@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *tipLb;
@property (nonatomic, strong) UIView *shishiView;
@property (nonatomic, strong) UIView *kuaiView;
@property (nonatomic, strong) UILabel *sumLb;
@property (nonatomic, strong) NSMutableArray<UIView *> *subVs;
@property (nonatomic, strong) NSMutableArray<UILabel *> *subLbs;

@end

@implementation HSAwardListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    self.subVs = @[].mutableCopy;
    self.subLbs = @[].mutableCopy;
    self.backgroundColor = WHITE_COLOR;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.width = ScreenWidth;
    self.nameLb = [UILabel labelWithFont:MYFont(Font34) color:HEXCOLOR(HSColorBlack)];
    [self addSubview:self.nameLb];
    self.timeLb = [UILabel labelWithFont:MYFont(Font28) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.timeLb];
    self.tipLb = [UILabel labelWithFont:MYFont(Font28) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.tipLb];
    UILabel *arrowLb = [UILabel labelWithFrame:CGRectZero font:IconFont2(22) color:HEXCOLOR(HSColorGray) text:@""];
    [self addSubview:arrowLb];
    self.shishiView = [UIView new];
    self.shishiView.clipsToBounds = YES;
    [self addSubview:self.shishiView];
    self.kuaiView = [UIView new];
    self.kuaiView.clipsToBounds = YES;
    [self addSubview:self.kuaiView];
    
    if (CPListType == 1) {
        UIView *line = [UIView new];
        line.backgroundColor = HEXCOLOR(HSColorBgLight);
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
        [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(8);
        }];
        [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLb.mas_right).offset(8);
            make.centerY.equalTo(self.nameLb.mas_centerY);
        }];
        [self.tipLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-20);
            make.centerY.equalTo(self.timeLb.mas_centerY);
        }];
        [self.shishiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(8);
            make.top.equalTo(self.nameLb.mas_bottom).offset(8);
            make.height.mas_equalTo(labelWid);
        }];
        [self.kuaiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.height.equalTo(self.shishiView);
        }];
    }
    else{
        [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(15);
        }];
        [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLb.mas_right).offset(8);
            make.centerY.equalTo(self.nameLb.mas_centerY);
        }];
        [self.tipLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.timeLb.mas_right).offset(8);
            make.centerY.equalTo(self.timeLb.mas_centerY);
        }];
        [self.shishiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.equalTo(self.nameLb.mas_bottom).offset(15);
            make.height.mas_equalTo(labelWid);
        }];
        [self.kuaiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.height.equalTo(self.shishiView);
        }];
        self.kuaiView.backgroundColor = HEXCOLOR(HSColorGreen);
        self.kuaiView.layer.cornerRadius = labelWid / 2.;
        [self showLineForDirection:MYDirectionBottom leftPad:10];
        
        self.sumLb = [UILabel labelWithFont:MYFont(Font28) color:HEXCOLOR(HSColorBlack)];
        [self addSubview:self.sumLb];
        [self.sumLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.kuaiView.mas_right).offset(10);
            make.centerY.equalTo(self.kuaiView);
        }];
    }
    [arrowLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-5);
        make.centerY.equalTo(self);
    }];
}

static const CGFloat labelWid = CPListType == 1 ? 25 : 30;
static const CGFloat kuaiLabelWid = 25;
static const CGFloat kuaiTop = CPListType == 1 ? 0 : 2.5;

- (void)setModel:(HSSubInfoModel *)model
{
    _model = model;
    self.nameLb.text = model.title;
    self.timeLb.text = model.prizeInfo.number;
    self.tipLb.text = [NSString stringWithFormat:@"全天%@期", model.num];
    if (model.prizeInfo.isKuai3) {
        self.sumLb.hidden = NO;
        CGFloat wid = CPListType == 1 ? 5 : 10;
        if (model.prizeInfo.data.count < 1) {
            UIView *view;
            if (self.subVs.count > 0) {
                view = self.subVs[0];
                UILabel *realLb = [view viewWithTag:2333];
                realLb.text = @"";
            }
            else{
                view = [self kuaiViewWithFrame:CGRectMake(wid, kuaiTop, kuaiLabelWid, kuaiLabelWid) title:@""];
                [self.kuaiView addSubview:view];
                [self.subVs addObject:view];
            }
            self.sumLb.text = @"和值：";
            [self.kuaiView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(kuaiLabelWid + wid + wid);
            }];
        }
        else{
            __block NSUInteger sum = 0;
            NSUInteger count = self.subVs.count;
            [model.prizeInfo.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                sum += [obj integerValue];
                UIView *view;
                if (idx >= count ) {
                    view = [self kuaiViewWithFrame:CGRectMake(wid + idx * (kuaiLabelWid + wid), kuaiTop, kuaiLabelWid, kuaiLabelWid) title:obj];
                    [self.kuaiView addSubview:view];
                    [self.subVs addObject:view];
                }
                else{
                    view = self.subVs[idx];
                    UILabel *realLb = [view viewWithTag:2333];
                    UIColor *color = BLACK_COLOR;
                    if ([obj isEqualToString:@"1"] || [obj isEqualToString:@"4"]) {
                        color = [UIColor redColor];
                    }
                    realLb.textColor = color;
                    realLb.text = [self saiziOfStr:obj];
                    [realLb sizeToFit];
                    realLb.center = CGPointMake(kuaiLabelWid / 2, kuaiLabelWid / 2);
                    if ([obj isEqualToString:@"1"]) {
                        realLb.top -= 2;
                    }
                    else if([obj isEqualToString:@"2"]){
                        realLb.top -= 4;
                    }
                }
            }];
            self.sumLb.text = [NSString stringWithFormat:@"和值：%@", sum > 0 ? @(sum) : @""];
        }
        [self.kuaiView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(model.prizeInfo.data.count * (kuaiLabelWid + wid) + wid);
        }];
        self.shishiView.hidden = YES;
        self.kuaiView.hidden = NO;
    }
    else{
        self.sumLb.hidden = YES;
        if (model.prizeInfo.data.count < 1) {
            UILabel *lb;
            if (self.subLbs.count > 0) {
                lb = self.subLbs[0];
                lb.text = @"";
            }
            else{
                lb = [self shiLabelWithFrame:CGRectMake(0, 0, labelWid, labelWid) title:@""];
                [self.shishiView addSubview:lb];
                [self.subLbs addObject:lb];
            }
            [self.shishiView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(labelWid + 3);
            }];
        }
        else{
            [model.prizeInfo.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UILabel *lb;
                if (idx >= self.subLbs.count) {
                    lb = [self shiLabelWithFrame:CGRectMake( idx * (labelWid + 3), 0, labelWid, labelWid) title:obj];
                    [self.shishiView addSubview:lb];
                    [self.subLbs addObject:lb];
                }
                else{
                    lb = self.subLbs[idx];
                    lb.text = obj;
                }
            }];
        }
        [self.shishiView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(model.prizeInfo.data.count * (labelWid + 3));
        }];
        self.kuaiView.hidden = YES;
        self.shishiView.hidden = NO;
    }
}

- (UILabel *)shiLabelWithFrame:(CGRect)frame title:(NSString *)title
{
    UILabel *lb = [UILabel labelWithFrame:frame font:MYFont(Font32) color:WHITE_COLOR text:title];
    lb.textAlignment = NSTextAlignmentCenter;
    lb.backgroundColor = [UIColor redColor];
    [lb makeRound:YES];
    return lb;
}

-(UIView *)kuaiViewWithFrame:(CGRect)frame title:(NSString *)title
{
    UIColor *color = BLACK_COLOR;
    if ([title isEqualToString:@"1"] || [title isEqualToString:@"4"]) {
        color = [UIColor redColor];
    }
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = WHITE_COLOR;
    view.layer.borderColor = HEXCOLOR(HSColorGray).CGColor;
    view.layer.borderWidth = 1;
    view.layer.cornerRadius = 4;
    
    UILabel *realLb = [UILabel labelWithFrame:CGRectZero font:IconFont2(65) color:color text:[self saiziOfStr:title]];
    realLb.tag = 2333;
    realLb.textAlignment = NSTextAlignmentCenter;
    [view addSubview:realLb];
    [realLb sizeToFit];
    realLb.center = CGPointMake(kuaiLabelWid / 2, kuaiLabelWid / 2);
    
    if ([title isEqualToString:@"1"]) {
        realLb.top -= 2;
    }
    else if([title isEqualToString:@"2"]){
        realLb.top -= 4;
    }
    
    return view;
}

- (NSString *)saiziOfStr:(NSString *)str
{
    if ([str isEqualToString:@"1"]) {
        return @"";
    }
    else if ([str isEqualToString:@"2"]) {
        return @"";
    }
    else if ([str isEqualToString:@"3"]) {
        return @"";
    }
    else if ([str isEqualToString:@"4"]) {
        return @"";
    }
    else if ([str isEqualToString:@"5"]) {
        return @"";
    }
    else if ([str isEqualToString:@"6"]) {
        return @"\U0000e9dc";
    }
    return @"";
}

@end
